<?php
class word {
    function start() {
        ob_start();
        print'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">';
    }

    function save($path) {
        print "</html>";
        $data = ob_get_contents();
        ob_end_clean();
        $this->wirtefile ($path,$data);
    }

    function wirtefile ($fn,$data) {
        $fp=fopen($fn,"wb");
        fwrite($fp,$data);
        fclose($fp);
    }
}

?>


<?php
$word=new word;
$word->start();

$dsn = 'mysql:dbname=v9;host=127.0.0.1';
$user = 'root';
$password = '';
$opt=array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'');

try {
    $pdo = new PDO($dsn, $user, $password,$opt );
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}
  $stmt= $pdo->prepare("SELECT id,content,question  FROM  `v9_question_data` ORDER BY  `v9_question_data`.`id` ASC  LIMIT 0 , 10");
  $stmt->execute();
  $row = $stmt->fetch(PDO::FETCH_ASSOC);

   while ($row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
   echo $row['id'];
   echo $row['question'];
   echo $row['content'];
  }

?>

<?php
$word->save("data.doc");//保存word并且结束.
?>
<a href="data.doc" target=_blank>这里</a>